<template>
  <div class="cart-container">
    <h2>购物车</h2>

    <div class="cart-title">
      <div class="subtitle">
        <div class="circular"></div>
        <div class="text">30天无忧退货</div>
      </div>
      <div class="subtitle">
        <div class="circular"></div>
        <div class="text">48小时快速退款</div>
      </div>
      <div class="subtitle">
        <div class="circular"></div>
        <div class="text">满88元免邮费</div>
      </div>
    </div>

    <div class="cart-cards-container">
      <div class="cart-cards" v-for="item in list" :key="item.id">
        <van-swipe-cell>
          <div class="left-box">
            <van-checkbox v-model="item.checked"></van-checkbox>
            <img :src="item.list_pic_url" />
            <div class="desc">
              <div class="title">{{ item.goods_name }}</div>
              <div class="priceAndCount">
                <span>&yen; {{ item.retail_price }}</span>
                <div class="stepper-container">
                  <van-stepper v-model="item.number" v-if="item.checked" />
                  <div v-else class="count">x {{ item.number }}</div>
                </div>
              </div>
            </div>
          </div>

          <template #right>
            <van-button
              square
              text="删除"
              type="danger"
              class="delete-button"
              @click="delgoods(item.id)"
            />
          </template>
        </van-swipe-cell>
      </div>
    </div>

    <div class="cart-footer">
      <van-checkbox v-model="allchecked">全选</van-checkbox>
      <div class="cart-footer-right">
        <span class="Total">合计:&nbsp;</span>
        <span class="price">&yen; {{ total }}</span>
        <van-button color="#CEA62A" class="buyBtn" @click="placeOrder"
          >下单</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { cartList, submitAction, deleteAction } from "@/api/Cart";
import { Dialog } from "vant";
export default {
  data() {
    return {
      list: [],
    };
  },
  methods: {
    async getlist() {
      let res = await cartList({ openId: localStorage.getItem("openId") });
      res.data.forEach((item) => {
        item.checked = false;
      });
      this.list = res.data;
    },
    async placeOrder() {
      if (this.total == 0) return;
      let arr = this.list.reduce((prev, next) => {
        if (next.checked) {
          prev.push(next.goods_id);
        }
        return prev;
      }, []);
      let res = await submitAction({
        allPrise: this.total,
        goodsId: arr.join(","),
        openId: localStorage.getItem("openId"),
      });
      if (res.data) this.$router.push("/pay");
    },
    delgoods(id) {
      Dialog.confirm({
        title: "提示",
        message: "删除后不可恢复,确定删除吗?",
      })
        .then(async () => {
          let res = await deleteAction({ id });
          if (res.data) {
            this.getlist();
          }
        })
        .catch(() => {
          this.$toast("取消删除");
        });
    },
  },
  computed: {
    allchecked: {
      get() {
        if (this.list.length == 0) {
          return false;
        }
        return this.list.every((item) => {
          return item.checked == true;
        });
      },
      set(value) {
        this.list.forEach((item) => {
          item.checked = value;
        });
      },
    },
    total() {
      return this.list.reduce((prev, next) => {
        if (next.checked) {
          prev = prev + next.number * next.retail_price;
        }
        return prev;
      }, 0);
    },
  },
  mounted() {
    this.getlist();
  },
};
</script>

<style lang="scss" scoped>
.cart-container {
  background-color: #f8f8f8;
  height: calc(100vh - 108px);
  h2 {
    text-align: center;
    padding: 9px 0 21px 0;
    height: 80px;
    font-size: 36px;
    color: #333333;
    letter-spacing: 0;
    font-weight: 500;
    border-bottom: 1px solid rgba(232, 232, 232, 1);
    background-color: #fff;
  }
  .cart-title {
    width: 100%;
    height: 105px;
    @include between;
    padding: 45px 20px 27px 20px;
    .subtitle {
      display: flex;
      align-items: center;
      .circular {
        width: 11px;
        height: 11px;
        border-radius: 50%;
        border: 3px solid rgba(183, 166, 115, 1);
        margin-right: 14px;
      }
      .text {
        font-size: 24px;
        color: #333333;
        letter-spacing: 0;
        font-weight: 400;
      }
    }
  }
  .cart-cards-container {
    padding: 0 20px;
    .cart-cards {
      height: 253px;
      background-color: #fff;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      .van-swipe-cell {
        width: 100%;
        height: 100%;
        ::v-deep .van-swipe-cell__wrapper {
          height: 100%;
        }
      }
      .left-box {
        width: 100%;
        height: 100%;
        @include center;
        img {
          width: 174px;
          height: 169px;
          margin: 0 35px 0 22px;
        }
      }
      .van-swipe-cell__right {
        height: 100%;
        .van-button {
          height: 100%;
        }
      }
      .desc {
        height: 100%;
        width: 381px;
        padding: 35px 0 27px 0;
        .title {
          font-size: 30px;
          color: #333333;
          height: 42px;
          font-weight: 400;
        }
        .priceAndCount {
          margin-top: 99px;
          height: 40px;
          @include between;
          span {
            font-size: 28px;
            color: #9e4242;
            letter-spacing: 0;
            font-weight: 500;
          }
          ::v-deep .van-stepper__input {
            margin: 0;
            background-color: #fff;
            border-left: 1px solid rgba(229, 229, 229, 1);
            border-right: 1px solid rgba(229, 229, 229, 1);
          }
          ::v-deep .van-stepper__minus--disabled,
          .van-stepper__plus--disabled {
            color: #333333;
            background-color: #fff;
          }
          .stepper-container {
            border: 1px solid rgba(229, 229, 229, 1);
            border-radius: 6px;
            .count{
             width: 47px;
             height: 40px;
             @include center;
            }
          }
        }
      }
    }
  }
  .cart-footer {
    background-color: #fff;
    width: 100%;
    height: 146px;
    position: fixed;
    bottom: 100px;
    left: 0;
    @include between;
    padding: 0 20px 0 25px;
    .van-checkbox__icon .van-icon {
      width: 40px;
      height: 40px;
    }
    ::v-deep .van-checkbox__label {
      font-size: 26px;
      color: #999999;
      letter-spacing: 0;
      font-weight: 400;
    }
    .cart-footer-right {
      display: flex;
      align-items: center;
      .total {
        font-size: 26px;
        color: #999999;
        letter-spacing: 0;
        font-weight: 400;
      }
      .price {
        font-size: 28px;
        color: #9e4242;
        letter-spacing: 0;
        font-weight: 500;
        margin-right: 38px;
      }
      .buyBtn {
        width: 193px;
        height: 72px;
        border-radius: 35.75px;
      }
    }
  }
}
</style>

<style lang="scss">
.cart-container {
  .cart-cards-container {
    .van-stepper__minus,
    .van-stepper__plus {
      background-color: #fff;
      color: #333333;
    }
  }
}
</style>

